$(function() {
	var selectedAlg=-1;
	$.getJSON('./reader', {
		'method' : 'getAllAlgs'
	}, function(data) {
		$.each(data, function(index, entry) {
			$("#algDetail").append(
					"<tr value= '" + entry['id'] + "'><td id='algid'>" + entry['id'] + "</td><td> "
							+ entry['name'] +"</td></tr>");
		});
		$("#algDetail tr").each(function() {
			var alg_id = $(this).children().filter("#algid").text();
			$(this).bind('click', function() {
				$("#algDetail tr").removeClass('hilite');
				$(this).toggleClass('hilite');
				click_alg(alg_id);
			}).css({'cursor':'pointer'});
		});

		$("#algorithm").change(function() {
			var xv = $("#algorithm").val();
			if (xv == "-1")
				return;
			else
				query_alg(xv);
		});
		$("#feature").change(function() {
			var xv = $("#feature").val();
			if (xv == "-1")
				return;
			else
				query_ftr(xv);
		});

		$("#featurebutton").click(function() {
			var fid = $("#featureInput").val();
			if (fid == "")
				return;
			else
				query_ftr(fid);
		});

	});

	function query_alg(alg_id) {
		$.getJSON('./reader', {
			method : 'getAlgInfo',
			id : alg_id
		},
				function(data) {
					$("#algorithm_desc").empty();
					$("#algorithm_desc").append(
							'<strong>算法设置:</strong><br>'
									+ data['Description'] + "<br><br>");
					$("#algorithm_desc").append(
							'<strong>输入:</strong><br>' + data['Input']
									+ "<br><br>");
					$("#algorithm_desc").append(
							'<strong>输出:</strong><br>' + data['Output']
									+ "<br><br>");
				});

		$
				.getJSON(
						'./reader',
						{
							method : 'getFeaturesByAlgID',
							id : alg_id
						},
						function(data) {
							$("#feature_query").empty();
							$("#featureInput").val("<<input feature>>");
							$.each(data, function(index, entry) {
								$("#feature_query").append(
										"<option value='" + entry + "'>"
												+ entry + "</option>");
							});
						});

	}

	function query_ftr(feature_id) {
		$.getJSON('./reader', {
			method : 'getFeatureByID',
			id : feature_id
		}, function(data) {
			$("tbody#tabcontent").empty();
			for ( var p in data) {
				if (typeof (data[p]) != "function") {
					$("tbody#tabcontent").append("<tr><td>"+p+"</td><td>"+data[p]+"</td></tr>");
				}
			}
		});
	}
	
	function click_alg(algid){
		query_alg(algid);
	}

});